/*
 * Copyright 2025 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.graphql.dgs.client

import com.netflix.graphql.dgs.client.scalar.DateRange
import com.netflix.graphql.dgs.client.scalar.DateRangeScalar
import graphql.GraphQLContext
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.time.LocalDate
import java.util.Locale

class RequestWithScalarTest {
    @Test
    fun `Creating a request with a date-time scalar as a variable should serialize correctly`() {
        val scalar = DateRangeScalar()
        val context = GraphQLContext.newContext().build()
        val locale = Locale.getDefault()
        val originalRange =
            DateRange(
                from = LocalDate.now(),
                to = LocalDate.now().plusDays(1),
            )

        val serialized = scalar.serialize(originalRange, context, locale)
        val variables = mapOf("currentDateTime" to serialized)

        val client =
            CustomGraphQLClient(
                url = "",
                requestExecutor = { _, _, _ -> HttpResponse(200, """{"data": null}""") },
            )
        val graphQLResponse =
            client.executeQuery(
                """
                query Calendar(${'$'}timePeriod: DateRange) {
                  getMeetings(timePeriod: ${'$'}timePeriod)
                }
                """.trimIndent(),
                variables,
            )

        assertThat(graphQLResponse).isNotNull

        // Verify round-trip: serialize then parse should give back the same dates
        val parsedRange = scalar.parseValue(serialized, context, locale)
        assertThat(parsedRange.from).isEqualTo(originalRange.from)
        assertThat(parsedRange.to).isEqualTo(originalRange.to)
    }
}
